﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class DBComd
    {
        public string strCon = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString.ToString();
        public System.Data.SqlClient.SqlConnection con;
        public SqlCommand com;

        public DataTable GetTData(string strsql)
        {
            try
            {
                con = new SqlConnection(strCon);
                con.Open();
                com = new SqlCommand(strsql, con);
                SqlDataReader dr = com.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Load(dr);
                con.Close();
                return dt;
            }
            catch (Exception ex)
            {

                return null;
            }

        }

        public DataSet GetSData(string strsql)
        {
            try
            {
                con = new SqlConnection(strCon);
                con.Open();
                SqlDataAdapter sda = new SqlDataAdapter(strsql, con);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                con.Close();
                return ds;
            }
            catch (Exception ex)
            {

                return null;
            }
        }

        public string GetData(string strsql)
        {
            try
            {
                con = new SqlConnection(strCon);
                con.Open();
                com = new SqlCommand(strsql, con);
                string data = "";
                if (com.ExecuteScalar() == null)
                {
                    data = "";
                }
                else
                {
                    data = com.ExecuteScalar().ToString();
                }

                con.Close();
                return data;
            }
            catch (Exception ex)
            {

                return "";
            }
        }

        public int Delete(string strsql)
        {
            try
            {
                con = new SqlConnection(strCon);
                con.Open();
                com = new SqlCommand(strsql, con);
                int i = com.ExecuteNonQuery();
                con.Close();
                return i;
            }
            catch (Exception ex)
            {

                return 0;
            }
        }

        public int Update(string strsql)
        {
            try
            {
                con = new SqlConnection(strCon);

                con.Open();

                com = new SqlCommand(strsql, con);
                int i = com.ExecuteNonQuery();
                con.Close();
                return i;
            }
            catch (Exception ex)
            {

                return 0;
            }
        }

        public int Insert(string strsql)
        {
            try
            {
                con = new SqlConnection(strCon);
                con.Open();
                com = new SqlCommand(strsql, con);
                int i = com.ExecuteNonQuery();
                con.Close();
                return i;

            }

            catch (Exception ex)
            {

                return 0;
            }
        }

    }
}
